#include <stdio.h>
#include <math.h>

// 定义min函数，用于计算两个整数中的最小值
int min(int a, int b) {
    return (a < b) ? a : b;
}

int main() {
    int n, dp[20010][2] = {0}, pre[2] = {1, 1}, now[2];

    scanf("%d", &n);

    for (int i = 1; i <= n; i++) {
        scanf("%d %d", &now[0], &now[1]);

        // 更新dp[i][0]
        dp[i][0] = min(dp[i-1][0] + abs(now[1] - pre[0]) + now[1] - now[0], dp[i-1][1] + abs(now[1] - pre[1]) + now[1] - now[0]) + 1;

        // 更新dp[i][1]
        dp[i][1] = min(dp[i-1][0] + abs(now[0] - pre[0]) + now[1] - now[0], dp[i-1][1] + abs(now[0] - pre[1]) + now[1] - now[0]) + 1;

        // 更新pre
        pre[0] = now[0];
        pre[1] = now[1];
    }

    // 输出dp
    for (int i = 0; i <= n; i++) {
        printf("%d %d\n", dp[i][0], dp[i][1]);
    }

    printf("%d", min(dp[n][0] + abs(n - pre[0]), dp[n][1] + abs(n - pre[1])) - 1);

    return 0;
}